<?php

namespace App\Http\Controllers;

use App\Http\Model\Api\AccompanyUser;
use App\Http\Model\Api\UserConvene;
use App\Http\Model\Config;
use App\Http\Model\OutJson;
use App\Http\Model\ShareDiamond;
use App\Http\Model\UserM;
use App\Http\Services\ShareDiamondServices;
use Laravel\Lumen\Routing\Controller as BaseController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;

/*
 * 充值赠送钻石
 * */

class ConveneController extends BaseController
{
    protected $model;
    protected $shareDiamondServices;
    protected $award_diamond = 3;//返利奖励比例 5%

    public function __construct()
    {
        $this->model = new AccompanyUser();
        $this->shareDiamondServices = new ShareDiamondServices();
    }

    public function assignAction($body)
    {
        $checkDate =  (bool) Redis::GET('CONVENE_SWITCH');
        if (!$checkDate) {
            write_log('conveneConsumer', 'timeError', $body);//活动时间错误
            return false;
        }
        $this->listenUserRecharge($body);// 充值
        return true;
    }

    public function listenUserRecharge($body)
    {
        //获取监听数据
//        $body = json_decode($body, true);
//        $body['user_id'] = $request->input('user_id');
//        $body['diamond_amount'] = 10000;
//        $body['price'] = 100;
//        $body['pay_type'] = 1;
//        $body['recharge_type'] = 0;
        $user_id = $body['user_id'] ?? 0;
        $diamond_num = $body['diamond_amount'] ?? 0;
        $pay_type = $body['pay_type'];
        $recharge_type = $body['recharge_type'] ?? 1;

        if($recharge_type != 0){
            write_log('convene', 'rechargeTypeError', json_encode($body));//金豆不计入
            return false;
        }
        // if($pay_type == 5){
        //     write_log('convene', 'payTypeError', json_encode($body));//代充不计入
        //     return false;
        // }
        //支付类型 --- 0.支付宝 1.微信 2.苹果内购 3.android公号 4.IOS公众号 5.后台代充 6.支付宝wap端充值 7.微信wap端充值
        if (empty($user_id) || empty($diamond_num)) {
            write_log('conevene', 'bodyError', json_encode($body));//paytype错
            return false;
        }

        $recharge_money = $body['price'];
        //获取用户关系
        $user = UserConvene::hasInvite($user_id);
        if(!$user) return false;

        $first_data['award_diamond'] = $diamond_num * $this->award_diamond / 100;
        $first_data['user_id'] = $user['invite_user_id'];
        //发送接口请求
        $recharge = [
            'user_id' => $first_data['user_id'],
            'diamond_num' => $first_data['award_diamond'],
            'msg' => '召集充值返利', 
        ];
        $diamond_api = json_decode(curl_request(env('KK_API_HOST') . '/finance/diamond/recharge', $recharge), true);
        if ($diamond_api['code'] != 0) {
            //充值失败 销毁队列
            write_log('convene', 'oneUserRechargeError', json_encode($diamond_api));
            return false;
        }
        //返回成功记录
        $this->shareDiamondServices->createDiamond($user_id, $user['invite_user_id'], 1, $recharge_money, $diamond_num, $first_data['award_diamond']);
        $msg_data_one['user_id'] = $user['invite_user_id'];
        $msg_data_one['msg'] = "召集充值返利 钻石* " . $first_data['award_diamond'];
        curl_request(env('KK_API_HOST') . '/msg/text', $msg_data_one);
        return true;
    }

    /**
     * 获取用户信息
     *
     * @param Request $request
     * @param integer $id
     * @param UserM $userM
     * @return void
     */
    public function userInfo(Request $request, int $id, UserM $userM, ShareDiamond $shareDiamond) {
      // 当前用户信息
      $user_info = $userM->getInviteUserInfo($id);

      // 获取当前用户的召集用户信息
      $be_called_info = $shareDiamond->getBeCalledInfo($id);

      // 统计当前用户累计收益、本月收益
      $total = $shareDiamond->statisticsMyCalledInfo($id);
      OutJson::toJson(200, '成功', [ 'user_info' => $user_info, 'be_called_info' => $be_called_info, 'grand_total' => $total['grand_total'], 'current_month_total' => $total['current_month_total'] ]);
    }

    /**
     * 他的召集信息
     *
     * @param Request $request
     * @param integer $id
     * @param ShareDiamond $shareDiamond
     * @return void
     */
    public function statisticsCalledInfo(Request $request, int $id, ShareDiamond $shareDiamond) {
      $list = $shareDiamond->statisticsCalledInfo($id);
      OutJson::toJson(200, '成功', $list);
    }

    /**
     * 充值返利明细
     *
     * @param Request $request
     * @param integer $id
     * @param ShareDiamond $shareDiamond
     * @return void
     */
    public function calledDetail(Request $request, int $id, ShareDiamond $shareDiamond) {
      $list = $shareDiamond->calledDetail($id);
      OutJson::toJson(200, '成功', $list);
    }

    public function init(Request $request){
      $isOpen = (bool) Redis::GET('CONVENE_SWITCH');
      OutJson::toJson(200, '成功', ['isStart' => $isOpen]);
    }

    /**
     * 根据邀请码获取用户信息
     *
     * @param Request $request
     * @param string $code
     * @param UserM $userM
     * @return void
     */
    public function getInviteInfo(Request $request,string $code, UserM $userM) {
      $info = $userM->getUserInfoByCode($code);
      OutJson::toJson(200, '成功', $info);
    }

    public function carousel(ShareDiamond $shareDiamond) {
      $list = $shareDiamond->carousel();
      OutJson::toJson(200, '成功', $list);
    }

    public function bindPhone(Request $request) {
      $code = $request->post('code', '');
      $phone = $request->post('phone', '');

      $res = curl_request(env('KK_API_HOST') . '/user/convene/create', [ 'code' => $code, 'phone'=> $phone ], 'urlencoded', 'GET');
      $res = json_decode($res, true);
      if($res['code'] === 0) {
        OutJson::toJson(200, '成功');
      }
      OutJson::toJson(201, $res['msg'] ?: '手机号不存在！');
    }
}
